package com.fzkjjt.shardingjdbc.service;

import com.fzkjjt.shardingjdbc.entity.FzUser;
import com.fzkjjt.shardingjdbc.entity.FzUserOrder;
import com.fzkjjt.shardingjdbc.mapper.FzUserOrderMapper;
import com.fzkjjt.shardingjdbc.mapper.UserMapper;
import io.shardingsphere.transaction.annotation.ShardingTransactionType;
import io.shardingsphere.transaction.api.TransactionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @program: sharding-jdbc
 * @description:
 * @author: FangZhen
 * @create: 2021-04-29 14:17
 **/
@Service
public class UserOrderService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private FzUserOrderMapper fzUserOrderMapper;

    @Transactional(rollbackFor = Exception.class) //针对单库
    @ShardingTransactionType(TransactionType.XA)  //针对分布式事务（跨库）
    public int saveUserOrder(FzUser user, FzUserOrder fzUserOrder) {
        userMapper.addUSer(user);
        fzUserOrder.setOrderid(user.getId());

        fzUserOrderMapper.addFzUserOder(fzUserOrder);
        return 1;
    }
}
